package com.example.xunua_zhinengjiaotong;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xunua_zhinengjiaotong.adapter.MyLogListViewAdapter;
import com.example.xunua_zhinengjiaotong.utils.ETCLogBean;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

public class ETCMessageActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView img_back;
    private TextView tv_log;
    private ListView listView;
    private HttpUtil util;
    private ArrayList<ETCLogBean> logList;
    private MyApp myApp;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                listView.setAdapter(new MyLogListViewAdapter(logList,ETCMessageActivity.this));
                tv_log.setVisibility(View.GONE);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_etcmessage);
        initView();
        getData();
    }

    private void getData() {
        //初始化 LogList
        logList=new ArrayList<>();
        logList.clear();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONArray getEtcListReport = util.connectServer_JsonArray("{}", myApp, "GetEtcListReport");
                    for (int i = 0; i < getEtcListReport.length(); i++) {
                        String str=getEtcListReport.getString(i);
                        JSONObject jsonObject=new JSONObject(str);
                        String money = jsonObject.getString("Money");
                        String carId = jsonObject.getString("CarId");
                        String etcInTime = jsonObject.getString("EtcInTime");
                        String etcOutTime = jsonObject.getString("EtcOutTime");
                        ETCLogBean bean=new ETCLogBean(money,carId,etcInTime,etcOutTime);
                        logList.add(bean);
                    }
                    Message message=new Message();
                    message.what=0;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView() {
        util=new HttpUtil();
        myApp= (MyApp) getApplication();
        img_back=findViewById(R.id.etcMessage_img_back);
        img_back.setOnClickListener(this);
        tv_log=findViewById(R.id.etcmessage_tv_log);
        listView=findViewById(R.id.etcmessage_lv);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.etcMessage_img_back:
                finish();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        // super.onBackPressed();
    }
}
